package com.web;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pojo.Employee;
import com.pojo.LeaveApply;
import com.pojo.OutApply;
import com.pojo.view.LeaveApplyCheck;
import com.pojo.view.OutApplyCheck;
import com.pojo.view.WaitToCheck;
import com.service.EmployeeService;
import com.service.LeaveApplyService;
import com.service.OutApplyService;
import com.service.impl.EmployeeServiceImpl;
import com.service.impl.LeaveApplyServiceImpl;
import com.service.impl.OutApplyServiceImpl;
import com.util.DateUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/managerServlet/*")
public class ManagerServlet extends BaseServlet {

    private LeaveApplyService leaveApplyService = new LeaveApplyServiceImpl();
    private OutApplyService outApplyService = new OutApplyServiceImpl();
    private EmployeeService employeeService = new EmployeeServiceImpl();

    /**
     * 查看请假申请
     * API接口说明
     * 无需接受参数
     * 返回为JSON数组
     * [
     * {
     * "applyTime": "2022-05-13 15:15:45",
     * "department": "技术部",
     * "endTime": "2022-05-19 15:00:00",
     * "leaveApplyId": 1,
     * "leaveReason": "朋友白事",
     * "name": "zhangsan",
     * "startTime": "2022-05-18 09:00:00",
     * "workId": "100005"
     * }
     * ]
     */
    public void checkLeaveApply(HttpServletRequest request, HttpServletResponse response) throws IOException {

        //状态为“正在审核”的LeaveApplyList
        List<LeaveApply> leaveApplyList = leaveApplyService.getLeaveApplyChecks();
        List<LeaveApplyCheck> leaveApplyCheckList = new ArrayList<>();

        for (LeaveApply leaveApply : leaveApplyList) {
            LeaveApplyCheck leaveApplyCheck = new LeaveApplyCheck();
            int leaveApplyId = leaveApply.getId();
            String applyTime = leaveApply.getApplyTime();
            String startTime = leaveApply.getStartTime();
            String endTime = leaveApply.getEndTime();
            String leaveReason = leaveApply.getLeaveReason();

            Employee employee = leaveApplyService.getEmployeeByLeaveApplyId(leaveApplyId);
            String workId = employee.getWorkId();
            String name = employee.getName();
            String department = employeeService.getDepartmentByWorkId(workId);

            leaveApplyCheck.setLeaveApplyId(leaveApplyId);
            leaveApplyCheck.setWorkId(workId);
            leaveApplyCheck.setName(name);
            leaveApplyCheck.setApplyTime(applyTime);
            leaveApplyCheck.setDepartment(department);
            leaveApplyCheck.setStartTime(startTime);
            leaveApplyCheck.setEndTime(endTime);
            leaveApplyCheck.setLeaveReason(leaveReason);

            leaveApplyCheckList.add(leaveApplyCheck);
        }

        String jsonString = JSON.toJSONString(leaveApplyCheckList);

        //设置跨域访问
        //允许跨域的主机地址
        response.setHeader("Access-Control-Allow-Origin", "*");
        //允许跨域的请求方法GET, POST, HEAD 等
        response.setHeader("Access-Control-Allow-Methods", "*");
        //重新预检验跨域的缓存时间 (s)
        response.setHeader("Access-Control-Max-Age", "4200");
        //允许跨域的请求头
        response.setHeader("Access-Control-Allow-Headers", "*");
        //是否携带cookie
        response.setHeader("Access-Control-Allow-Credentials", "true");

        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    /**
     * 同意请假申请
     * API接口说明
     * POST方法 参数为apply_time
     * {"apply_time":"2022-05-13 15:15:45"}
     * 成功返回"update status successfully"
     * 不成功返回"no such a applyTime"
     */
    public void approveLeaveApply(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        BufferedReader bufferedReader = request.getReader();
        String params = bufferedReader.readLine();
        JSONObject jsonObject = JSON.parseObject(params);

        String applyTime = jsonObject.get("apply_time").toString();

        //设置跨域访问
        //允许跨域的主机地址
        response.setHeader("Access-Control-Allow-Origin", "*");
        //允许跨域的请求方法GET, POST, HEAD 等
        response.setHeader("Access-Control-Allow-Methods", "*");
        //重新预检验跨域的缓存时间 (s)
        response.setHeader("Access-Control-Max-Age", "4200");
        //允许跨域的请求头
        response.setHeader("Access-Control-Allow-Headers", "*");
        //是否携带cookie
        response.setHeader("Access-Control-Allow-Credentials", "true");


        try {
            int leaveApplyId = leaveApplyService.getLeaveApplyIdByApplyTime(applyTime);

            //更新对应LeaveApply的状态为“已通过”
            leaveApplyService.updateStatusToApprovedById(leaveApplyId);

            //根据LeaveApplyId得到请假的相隔天数
            LeaveApply leaveApply = leaveApplyService.getLeaveApplyByLeaveApplyId(leaveApplyId);
            String startTime = leaveApply.getStartTime();
            String endTime = leaveApply.getEndTime();
            int apartDay = DateUtils.getApartDay(startTime, endTime);

            //更改对应employee的剩余年假天数
            Employee employee = leaveApplyService.getEmployeeByLeaveApplyId(leaveApplyId);
            String workId = employee.getWorkId();
            employeeService.setAnnualLeaveRemainByWorkId(workId, apartDay);

            response.getWriter().write("update status successfully");
        } catch (Exception e) {
            response.getWriter().write("no such a applyTime");
        }

    }


    /**
     * 查看外出申请
     * API接口说明
     * 无需接受参数
     * 返回为JSON数组
     * [
     * {
     * "applyTime": "2022-05-13 15:15:45",
     * "department": "技术部",
     * "endTime": "2022-05-19 15:00:00",
     * "outApplyId": 1,
     * "outReason": "部门外派",
     * "name": "zhangsan",
     * "startTime": "2022-05-18 09:00:00",
     * "workId": "100005"
     * }
     * ]
     */
    public void checkOutApply(HttpServletRequest request, HttpServletResponse response) throws IOException {

        //状态为“正在审核”的OutApplyList
        List<OutApply> outApplyList = outApplyService.getOutApplyChecks();
        List<OutApplyCheck> outApplyCheckList = new ArrayList<>();

        //设置跨域访问
        //允许跨域的主机地址
        response.setHeader("Access-Control-Allow-Origin", "*");
        //允许跨域的请求方法GET, POST, HEAD 等
        response.setHeader("Access-Control-Allow-Methods", "*");
        //重新预检验跨域的缓存时间 (s)
        response.setHeader("Access-Control-Max-Age", "4200");
        //允许跨域的请求头
        response.setHeader("Access-Control-Allow-Headers", "*");
        //是否携带cookie
        response.setHeader("Access-Control-Allow-Credentials", "true");


        for (OutApply outApply : outApplyList) {
            OutApplyCheck outApplyCheck = new OutApplyCheck();
            int outApplyId = outApply.getId();
            String applyTime = outApply.getApplyTime();
            String startTime = outApply.getStartTime();
            String endTime = outApply.getEndTime();
            String outReason = outApply.getOutReason();

            Employee employee = outApplyService.getEmployeeByOutApplyId(outApplyId);
            String workId = employee.getWorkId();
            String name = employee.getName();
            String department = employeeService.getDepartmentByWorkId(workId);

            outApplyCheck.setOutApplyId(outApplyId);
            outApplyCheck.setWorkId(workId);
            outApplyCheck.setName(name);
            outApplyCheck.setApplyTime(applyTime);
            outApplyCheck.setDepartment(department);
            outApplyCheck.setStartTime(startTime);
            outApplyCheck.setEndTime(endTime);
            outApplyCheck.setOutReason(outReason);

            outApplyCheckList.add(outApplyCheck);
        }

        String jsonString = JSON.toJSONString(outApplyCheckList);

        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    /**
     * 同意外出申请
     * API接口说明
     * POST方法 参数为apply_time
     * {"apply_time":"2022-05-13 15:15:45"}
     * 成功返回"update status successfully"
     * 不成功返回"no such a applyTime"
     */
    public void approveOutApply(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        BufferedReader bufferedReader = request.getReader();
        String params = bufferedReader.readLine();
        JSONObject jsonObject = JSON.parseObject(params);

        String applyTime = jsonObject.get("apply_time").toString();

        //设置跨域访问
        //允许跨域的主机地址
        response.setHeader("Access-Control-Allow-Origin", "*");
        //允许跨域的请求方法GET, POST, HEAD 等
        response.setHeader("Access-Control-Allow-Methods", "*");
        //重新预检验跨域的缓存时间 (s)
        response.setHeader("Access-Control-Max-Age", "4200");
        //允许跨域的请求头
        response.setHeader("Access-Control-Allow-Headers", "*");
        //是否携带cookie
        response.setHeader("Access-Control-Allow-Credentials", "true");


        try {
            int outApplyId = outApplyService.getOutApplyIdByApplyTime(applyTime);

            //更新对应OutApply的状态为“已通过”
            outApplyService.updateStatusToApprovedById(outApplyId);

            //根据OutApplyId得到外出的相隔天数
            OutApply outApply = outApplyService.getOutApplyByOutApplyId(outApplyId);
            String startTime = outApply.getStartTime();
            String endTime = outApply.getEndTime();
            int apartDay = DateUtils.getApartDay(startTime, endTime);

            //更改对应employee的剩余年假天数
            Employee employee = outApplyService.getEmployeeByOutApplyId(outApplyId);
            String workId = employee.getWorkId();
            employeeService.setAnnualLeaveRemainByWorkId(workId, apartDay);

            response.getWriter().write("update status successfully");
        } catch (Exception e) {
            response.getWriter().write("no such a applyTime");
        }
    }

    /**
     * 查看等待审核的LeaveApply和OutApply
     * API接口说明
     * GET方法 无需参数
     * 返回示例:{"leaveApplyWait":1,"outApplyWait":0}
     */
    public void getWaitToCheck(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer waitToCheckOfLeaveApply = leaveApplyService.getWaitToCheckOfAll();
        Integer waitToCheckOfOutApply = outApplyService.getWaitToCheckOfAll();

        WaitToCheck waitToCheck = new WaitToCheck();
        if (waitToCheckOfLeaveApply != null) {
            waitToCheck.setLeaveApplyWait(waitToCheckOfLeaveApply);
        } else {
            waitToCheck.setLeaveApplyWait(0);
        }
        if (waitToCheckOfOutApply != null) {
            waitToCheck.setOutApplyWait(waitToCheckOfOutApply);
        } else {
            waitToCheck.setOutApplyWait(0);
        }

        //设置跨域访问
        //允许跨域的主机地址
        response.setHeader("Access-Control-Allow-Origin", "*");
        //允许跨域的请求方法GET, POST, HEAD 等
        response.setHeader("Access-Control-Allow-Methods", "*");
        //重新预检验跨域的缓存时间 (s)
        response.setHeader("Access-Control-Max-Age", "4200");
        //允许跨域的请求头
        response.setHeader("Access-Control-Allow-Headers", "*");
        //是否携带cookie
        response.setHeader("Access-Control-Allow-Credentials", "true");


        String toJSONString = JSON.toJSONString(waitToCheck);
        response.getWriter().write(toJSONString);
    }


    /**
     * 拒绝请假申请
     * API接口说明
     * POST方法 参数为apply_time
     * {"apply_time":"2022-05-13 15:15:45"}
     * 成功返回"refuse leaveapply successfully"
     * 不成功返回"no such a applyTime"
     */
    public void refuseLeaveApply(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        BufferedReader bufferedReader = request.getReader();
        String params = bufferedReader.readLine();
        JSONObject jsonObject = JSON.parseObject(params);

        String applyTime = jsonObject.get("apply_time").toString();

        //设置跨域访问
        //允许跨域的主机地址
        response.setHeader("Access-Control-Allow-Origin", "*");
        //允许跨域的请求方法GET, POST, HEAD 等
        response.setHeader("Access-Control-Allow-Methods", "*");
        //重新预检验跨域的缓存时间 (s)
        response.setHeader("Access-Control-Max-Age", "4200");
        //允许跨域的请求头
        response.setHeader("Access-Control-Allow-Headers", "*");
        //是否携带cookie
        response.setHeader("Access-Control-Allow-Credentials", "true");


        try {
            int leaveApplyId = leaveApplyService.getLeaveApplyIdByApplyTime(applyTime);

            //更新对应LeaveApply的状态为“已通过”
            leaveApplyService.updateStatusToRefusedById(leaveApplyId);

            response.getWriter().write("refuse leaveapply successfully");
        } catch (Exception e) {
            response.getWriter().write("no such a applyTime");
        }

    }


    /**
     * 拒绝外出申请
     * API接口说明
     * POST方法 参数为apply_time
     * {"apply_time":"2022-05-13 15:15:45"}
     * 成功返回"refuse outapply successfully"
     * 不成功返回"no such a applyTime"
     */
    public void refuseOutApply(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        BufferedReader bufferedReader = request.getReader();
        String params = bufferedReader.readLine();
        JSONObject jsonObject = JSON.parseObject(params);

        String applyTime = jsonObject.get("apply_time").toString();

        //设置跨域访问
        //允许跨域的主机地址
        response.setHeader("Access-Control-Allow-Origin", "*");
        //允许跨域的请求方法GET, POST, HEAD 等
        response.setHeader("Access-Control-Allow-Methods", "*");
        //重新预检验跨域的缓存时间 (s)
        response.setHeader("Access-Control-Max-Age", "4200");
        //允许跨域的请求头
        response.setHeader("Access-Control-Allow-Headers", "*");
        //是否携带cookie
        response.setHeader("Access-Control-Allow-Credentials", "true");


        try {
            int outApplyId = outApplyService.getOutApplyIdByApplyTime(applyTime);

            //更新对应OutApply的状态为“已通过”
            outApplyService.updateStatusToRefusedById(outApplyId);

            response.getWriter().write("refuse outapply successfully");
        } catch (Exception e) {
            response.getWriter().write("no such a applyTime");
        }

    }

}
